package com.hx.petsever.controller;

import com.hx.petsever.config.Result;
import com.hx.petsever.entity.Address;
import com.hx.petsever.entity.Cart;
import com.hx.petsever.service.CartService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    CartService cartService;

    @RequestMapping("/add")
    public Result insert(Cart cart)
    {
        Result<Object> objectResult = new Result<>();
        HashMap hashMap = cartService.Insert(cart.getUserid(),cart.getGoodid(),cart.getNum());
        objectResult.success(hashMap.get("data").toString());
        return objectResult;
    }

    @RequestMapping("/getAll")
    public Result getAll(@Param("userid") String userid)
    {
        Result<Object> objectResult = new Result<>();
        HashMap<String,Object> map = cartService.getAllbyId(userid);
        if(map.get("state").toString().equals("0"))
            objectResult.fail(map.get("msg").toString());
        else
        {
            objectResult.setData(map.get("msg"));
            objectResult.success();
        }
        return objectResult;
    }

    @RequestMapping("/updataCart")
    public Result updataCart(@Param("userid") String userid , @Param("goodid") String goodid,@Param("num") String num)
    {
        Result<Object> objectResult = new Result<>();
        HashMap<String,Object> map = cartService.update(userid,goodid,num);
        if(map.get("state").toString().equals("0"))
            objectResult.fail(map.get("msg").toString());
        else
        {
            objectResult.setData(map.get("msg"));
            objectResult.success();
        }
        return objectResult;
    }

    @RequestMapping("/delete")
    public Result deleteOne(Cart cart)
    {
        Result<Object> objectResult = new Result<>();
        HashMap hashMap = cartService.DeleteOne(cart.getUserid(),cart.getGoodid(),cart.getNum());
        objectResult.success(hashMap.get("msg").toString());
        return objectResult;
    }
}
